package ahblue.cn.tanzi.ui.circle;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.data.source.ApiCallback;
import ahblue.cn.tanzi.data.source.RemoteRepository;
import ahblue.cn.tanzi.model.circle.Circle;
import ahblue.cn.tanzi.ui.base.BaseActivity;
import ahblue.cn.tanzi.util.GlideApp;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的圈子界面，当用户没有加入圈子时的入口界面
 */
public class CircleMyselfNullActivity extends BaseActivity {

    @BindView(R.id.recyclerViewCircle)
    RecyclerView mRecyclerViewCircle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_circle_myself_null);

        ButterKnife.bind(this);

        initData();
    }

    private void initData() {
        RemoteRepository repo = RemoteRepository.getsInstance();
        repo.getBigShotCircleThreeCircle(0, 3, mThreeCircleCallback);
    }

    @OnClick(R.id.btn_back)
    void onClickButtonBack() {
        finish();
    }

    /**
     * 单击"换一批"的事件处理
     */
    @OnClick(R.id.rl_change)
    void onClickChangeCircle() {
        initData();
    }

    /**
     * 单击"显示所有圈子"的事件处理
     */
    @OnClick(R.id.tv_mycircle_all)
    void onClickShowAllCircle() {
        final Intent intent = new Intent(this, CircleShowAllActivity.class);
        startActivity(intent);
    }

    ApiCallback<List<Circle>> mThreeCircleCallback = new ApiCallback<List<Circle>>() {

        @Override
        public void onSuccess(List<Circle> data) {
            CircleRandomAdapter adapter = new CircleRandomAdapter(data);
            adapter.setOnItemChildClickListener(mOnClickAdd2Circle);

            mRecyclerViewCircle.setLayoutManager(new LinearLayoutManager(CircleMyselfNullActivity.this));
            mRecyclerViewCircle.setAdapter(adapter);
        }

        @Override
        public void onFailed(String errorMsg) {

        }
    };

    class CircleRandomAdapter extends BaseQuickAdapter<Circle, BaseViewHolder> {

        CircleRandomAdapter(@Nullable List<Circle> data) {
            super(R.layout.item_circle_recommend, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, Circle item) {
            final ImageView iv = helper.getView(R.id.img_head);
            GlideApp.with(iv).load(item.head_portrait)
                    .placeholder(R.drawable.user_default_avatar)
                    .error(R.drawable.user_default_avatar)
                    .into(iv);

            helper.setText(R.id.tv_name, item.circle_name);
            helper.setText(R.id.tv_member_count, "成员数 " + Integer.toString(item.member_count)
                + " 动态数 " + item.dynamic_count);
            helper.setText(R.id.tv_intro, item.simple_introduce);

            helper.addOnClickListener(R.id.tv_join);
        }
    }

    BaseQuickAdapter.OnItemChildClickListener mOnClickAdd2Circle = new BaseQuickAdapter.OnItemChildClickListener() {

        @Override
        public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
            final Circle circle = (Circle) adapter.getData().get(position);
            final Intent intent = new Intent(CircleMyselfNullActivity.this, CircleDetailActivity.class);
            intent.putExtra(CircleDetailActivity.ARG_CIRCLE_ID, circle.id);

            CircleMyselfNullActivity.this.startActivity(intent);
        }
    };
}

